﻿///***************************************************************
/// 시  스  템 : Updms.Ref
/// 단위시스템 : REF
/// 계      층 : Presentation Layer
/// 프로그램명 : Ref2010r
/// 파  일  명 : Ref2010r.aspx.cs
/// 설      명 : 활력챠트
///***************************************************************
/// 수정ID 일자       이름     내용
///---------------------------------------------------------------
/// CH001  2013-05-03 송시명   최초 작성
///***************************************************************
using System;
using System.Data;
using System.Collections;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using SMFramework.Data;
using SMFramework.Utility;
using SMFramework.Web.UI.WebControls.Page;

using Base.AppFramework.Web.UI.CommandBar;
using Base.Dac;

using Updms.Ref.Exe;

namespace Updms.Ref
{
	public partial class Ref2010r : Updms.Common.BaseClass.PageBase
	{
		#region 전역영역

		public static readonly string PID = "Ref2010r";
		public static readonly string gs_path_info = "/updms/Ref/" + PID + ".aspx";

		Global global = new Global();
		CommCode.Com com = new CommCode.Com(LANG_SET);
		CommCode.Etc etc = new CommCode.Etc(LANG_SET);

		//검색
		protected string kw1 = "";	//업소
		protected string kw2 = "";	//이름
		protected string kw3 = "";	//기준년월
		protected string kw4 = "";	//홈런
		protected string kw5 = "";	//만족도
		protected string kw6 = "";	//내용
		protected string kw7 = "";	//키워드

		//메뉴 선택값
		protected string gs_sm = "";

		#endregion

		#region Page_Load, OnPageLoad

		protected void Page_Load(object sender, EventArgs e)
		{
			SetInit();
			if (!Page.IsPostBack)
			{
				// 최초 화면 로딩시 처리해야 할 부분
				OnPageLoad();
				SearchProcess();
			}
			else
			{
				CallJavaScriptMethodInAjax("setResize();");
			}
			SetClientScriptNoPostBack();
		}

		// OnPageLoad
		private void OnPageLoad()
		{
			SetLiterals(global.GetLabelInfoByPgmId(PID, LANG_SET));
			SetLayout();
			SetClientScript();
			SetSearchShopId();
			SetSearchHomeRunYn();
			SetBaseYm();
			SetSearchParameter();
		}

		#endregion

		#region InitializeComponent

		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
			this.PreRender += new System.EventHandler(this.Page_PreRender);
		}

		#endregion

		#region SetClientScript, SetClientScriptNoPostBack

		private void SetClientScript()
		{
			//onKeyPress이벤트 (엔터검색)
			txt_search_nick_nm.Attributes.Add("onkeypress", "enterSearchOnPage(event)");
			txt_search_contents.Attributes.Add("onkeypress", "enterSearchOnPage(event)");
			txt_search_keyword.Attributes.Add("onkeypress", "enterSearchOnPage(event)");
			txt_admin_pw.Attributes.Add("onkeypress", "enterSearchAdminPw(event)");
		}

		//포스트백과 무관하게 이벤트 지정
		private void SetClientScriptNoPostBack()
		{
			cbl_search_shop_id.Items[0].Attributes.Add("onclick", "checkBoxListCheck('cbl_search_shop_id');");
		}

		#endregion

		#region SetInit, SetSearchParameter

		//초기값설정
		private void SetInit()
		{
			lbl_search_flag.Text = "INIT";

			gs_sm = (Request.QueryString["sm"] == null || Request.QueryString["sm"].Trim().Length != 7)
					? global.GetDirDepth(PID) : Request.QueryString["sm"].ToString();
		}

		//검색파라미터
		private void SetSearchParameter()
		{
			switch (lbl_search_flag.Text.Trim().ToUpper())
			{
				case "INIT":
					if (String.IsNullOrEmpty(Request.QueryString["kw1"]))
					{
						kw1 = WebControlUtil.GetCheckBoxListValue(cbl_search_shop_id, 1);
					}
					else
					{
						kw1 = Server.UrlDecode(Request.QueryString["kw1"]);
						WebControlUtil.SetCheckBoxListValue(cbl_search_shop_id, kw1);
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw2"]))	// url에 값이 없으면
					{
						kw2 = txt_search_nick_nm.Text.Trim();
					}
					else
					{
						kw2 = Server.UrlDecode(Request.QueryString["kw2"]);
						txt_search_nick_nm.Text = kw2;
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw3"]))	// url에 값이 없으면
					{
						kw3 = ddl_search_base_year.SelectedValue + "-" + ddl_search_base_month.SelectedValue;
					}
					else
					{
						kw3 = Server.UrlDecode(Request.QueryString["kw3"]);
						ddl_search_base_year.SelectedValue = kw3.Substring(0, 4).ToString();
						ddl_search_base_month.SelectedValue = kw3.Substring(5, 2).ToString();
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw4"]))	// url에 값이 없으면
					{
						kw4 = ddl_search_homerun_yn.SelectedValue;
					}
					else
					{
						kw4 = Server.UrlDecode(Request.QueryString["kw4"]);
						ddl_search_homerun_yn.SelectedValue = kw4;
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw5"]))	// url에 값이 없으면
					{
						kw5 = txt_search_satisfaction.Text;
					}
					else
					{
						kw5 = Server.UrlDecode(Request.QueryString["kw5"]);
						txt_search_satisfaction.Text = kw5;
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw6"]))	// url에 값이 없으면
					{
						kw6 = txt_search_contents.Text.Trim();
					}
					else
					{
						kw6 = Server.UrlDecode(Request.QueryString["kw6"]);
						txt_search_contents.Text = kw6;
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw7"]))	// url에 값이 없으면
					{
						kw7 = txt_search_keyword.Text.Trim();
					}
					else
					{
						kw7 = Server.UrlDecode(Request.QueryString["kw7"]);
						txt_search_keyword.Text = kw7;
					}

					break;
				case "SEARCH":
					kw1 = WebControlUtil.GetCheckBoxListValue(cbl_search_shop_id, 1);
					kw2 = txt_search_nick_nm.Text.Trim();
					kw3 = ddl_search_base_year.SelectedValue + "-" + ddl_search_base_month.SelectedValue;
					kw4 = ddl_search_homerun_yn.SelectedValue;
					kw5 = txt_search_satisfaction.Text;
					kw6 = txt_search_contents.Text.Trim();
					kw7 = txt_search_keyword.Text.Trim();
					break;
			}
		}

		#endregion

		#region 마스터 Method (SetBaseYm, SetSearchShopId, SetSearchUseYn)

		//레이아웃
		private void SetLayout()
		{
			etc.SetLayout(rbl_layout);
		}

		//기준년도 설정
		private void SetBaseYm()
		{
			int li_curr_year = Convert.ToInt32(DateUtil.GetYearEx(0, "yyyy"));

			for (int i = 2004; i <= li_curr_year; i++)
			{
				string ls_year = i.ToString();
				ddl_search_base_year.Items.Add(new ListItem(ls_year, ls_year));
			}

			//기본값
			ddl_search_base_year.SelectedValue = DateUtil.GetYearEx(0, "yyyy");
			ddl_search_base_month.SelectedValue = DateUtil.GetMonthEx(0, "MM");
		}

		private void SetSearchShopId()
		{
			DataSet lo_ds = new DataSet();
			Ref1010m_NT _Ref1010m_NT = new Ref1010m_NT();

			lo_ds.Merge(_Ref1010m_NT.GetShopInfoList());

			cbl_search_shop_id.Items.Clear();
			cbl_search_shop_id.DataSource = lo_ds;
			cbl_search_shop_id.DataMember = "UPDMS_REF_SHOP";
			cbl_search_shop_id.DataTextField = "Shop_Nm";
			cbl_search_shop_id.DataValueField = "Shop_Id";
			cbl_search_shop_id.DataBind();

			cbl_search_shop_id.Items.Insert(0, new ListItem(GetXmlMsg("STR_019"), "ALL"));	//전체선택
		}

		//홈런여부
		private void SetSearchHomeRunYn()
		{
			com.SetUseYn(ddl_search_homerun_yn);
			ddl_search_homerun_yn.Items.Insert(0, new ListItem(DDL_SELECT, ""));
		}

		#endregion

		#region 디테일 Method (SetPrevNextMonth, SetEnabledMonthBtn, SetThisMonth, SetPrevNextYear, SetEnabledYearBtn, SetThisYear, SetAdminMode)

		//이전달, 다음달버튼 누르면
		private void SetPrevNextMonth(int pi_month_cnt)
		{
			string ls_ym = ddl_search_base_year.SelectedValue + "-" + ddl_search_base_month.SelectedValue;
			string ls_calc_ym = DateUtil.GetCalcMonth(ls_ym, pi_month_cnt, "yyyy-MM");

			if (WebControlUtil.CheckDdlValue(ddl_search_base_year, ls_calc_ym.Substring(0, 4).ToString()))
			{
				ddl_search_base_year.SelectedValue = ls_calc_ym.Substring(0, 4).ToString();
				ddl_search_base_month.SelectedValue = ls_calc_ym.Substring(5, 2).ToString();
			}

			//버튼처리
			SetEnabledMonthBtn();
		}

		//이전달, 다음달버튼 처리
		private void SetEnabledMonthBtn()
		{
			ibtn_prev_month.Enabled
				= (ddl_search_base_year.SelectedIndex == 0 && ddl_search_base_month.SelectedIndex == 0) ? false : true;

			ibtn_next_month.Enabled
				= (ddl_search_base_year.SelectedIndex == ddl_search_base_year.Items.Count - 1 && ddl_search_base_month.SelectedIndex == 11) ? false : true;
		}

		//당월설정
		private void SetThisMonth()
		{
			string ls_this_ym = DateUtil.GetNow("yyyy-MM");
			ddl_search_base_year.SelectedValue = ls_this_ym.Substring(0, 4).ToString();
			ddl_search_base_month.SelectedValue = ls_this_ym.Substring(5, 2).ToString();
			SetEnabledMonthBtn();
		}

		//이전년, 다음년버튼 누르면
		private void SetPrevNextYear(int pi_year_cnt)
		{
			int li_calc_year = Convert.ToInt32(ddl_search_base_year.SelectedValue) + pi_year_cnt;	//2008 + (-1)

			if (WebControlUtil.CheckDdlValue(ddl_search_base_year, li_calc_year.ToString()))
			{
				ddl_search_base_year.SelectedValue = li_calc_year.ToString();
			}

			//버튼처리
			SetEnabledYearBtn();
		}

		//이전년, 다음년버튼 처리
		private void SetEnabledYearBtn()
		{
			ibtn_prev_year.Enabled
				= (ddl_search_base_year.SelectedIndex == 0) ? false : true;

			ibtn_next_year.Enabled
				= (ddl_search_base_year.SelectedIndex == ddl_search_base_year.Items.Count - 1) ? false : true;
		}

		//당년설정
		private void SetThisYear()
		{
			string ls_this_year = DateUtil.GetNow("yyyy");
			ddl_search_base_year.SelectedValue = ls_this_year;
			SetEnabledYearBtn();
		}

		//관리자화면
		private void SetAdminMode(string ls_div)
		{
			if (REF_OPT_02 == "Y")	//관리자모드 사용여부
			{
				switch (ls_div)
				{
					case "VIEW":
						div_admin_pw.Visible = true;
						txt_contents.Visible = false;
						lbl_keyword.Visible = false;
						break;
					case "ADMN":
						div_admin_pw.Visible = false;
						txt_contents.Visible = true;
						lbl_keyword.Visible = true;
						break;
				}
			}
		}

		#endregion

		#region CRUD (SearchProcess, ViewDetail)

		//검색
		private void SearchProcess()
		{
			DataPack lo_dp = new DataPack();
			DataSet lo_ds = new DataSet();

			lbl_search_flag.Text = "SEARCH";
			SetSearchParameter();

			lo_dp.AddProperty("ls_shop_id", typeof(string), kw1);
			lo_dp.AddProperty("ls_nick_nm", typeof(string), kw2);
			lo_dp.AddProperty("ls_base_ym", typeof(string), kw3);
			lo_dp.AddProperty("ls_homerun_yn", typeof(string), kw4);
			lo_dp.AddProperty("ls_satisfaction", typeof(string), kw5);
			lo_dp.AddProperty("ls_contents", typeof(string), kw6);
			lo_dp.AddProperty("ls_keyword", typeof(string), kw7);

			using (Ref2010r_NT _Ref2010r_NT = new Ref2010r_NT())
			{
				if (rbl_layout.SelectedValue == "AH001")
				{
					lo_ds.Merge(_Ref2010r_NT.GetMonthlyTour(lo_dp));
				}
				else
				{
					lo_ds.Merge(_Ref2010r_NT.GetYearlyTour(lo_dp));
				}
			}

			GridView1.DataSource = lo_ds;
			GridView1.DataBind();

			switch (rbl_layout.SelectedValue)
			{
				case "AH001":	//월별
					tabel_btn_month.Visible = true;
					tabel_btn_year.Visible = false;
					lbl_this_month.Text = DateUtil.ConvertDateMask(kw3, "yyyy-MM", LANG_SET);
					arrow_top.Visible = false;
					SetEnabledMonthBtn();
					break;
				case "AH002":	//년도별
					tabel_btn_month.Visible = false;
					tabel_btn_year.Visible = true;
					lbl_this_year.Text = DateUtil.ConvertDateMask(kw3, "yyyy", LANG_SET);
					arrow_top.Visible = true;
					SetEnabledYearBtn();
					break;
				default:
					tabel_btn_month.Visible = true;
					tabel_btn_year.Visible = false;
					lbl_this_month.Text = DateUtil.ConvertDateMask(kw3, "yyyy-MM", LANG_SET);
					arrow_top.Visible = false;
					SetEnabledMonthBtn();
					break;
			}

			ClearDetail();
		}

		//상세보기
		private void ViewDetail(int seq)
		{
			DataPack lo_dp = new DataPack();
			DataSet lo_ds = new DataSet();

			lo_dp.AddProperty("li_seq", typeof(int), seq);

			using (Ref2010r_NT _Ref2010r_NT = new Ref2010r_NT())
			{
				lo_ds.Merge(_Ref2010r_NT.GetTourInfoBySeq(lo_dp));
			}

			if (DataSetUtil.CheckTable(lo_ds, "UPDMS_REF_REPORT"))
			{
				if (DataSetUtil.GetRowCount(lo_ds, "UPDMS_REF_REPORT") > 0)
				{
					lbl_shop_nm.Text = DataSetUtil.GetFldStr(lo_ds, "UPDMS_REF_REPORT", "Shop_Nm");
					lbl_nick_nm.Text = DataSetUtil.GetFldStr(lo_ds, "UPDMS_REF_REPORT", "Nick_Nm");
					lbl_tour_dt.Text = DataSetUtil.GetFldStr(lo_ds, "UPDMS_REF_REPORT", "Tour_Dt");
					lbl_tour_time.Text = DataSetUtil.GetFldStr(lo_ds, "UPDMS_REF_REPORT", "Tour_Time");
					lbl_place.Text = DataSetUtil.GetFldStr(lo_ds, "UPDMS_REF_REPORT", "Place");
					lbl_location.Text = DataSetUtil.GetFldStr(lo_ds, "UPDMS_REF_REPORT", "NaverMap_Link");
					lbl_price.Text = ConversionUtil.AddComma(DataSetUtil.GetFldStr(lo_ds, "UPDMS_REF_REPORT", "Price"));
					lbl_homerun_yn.Text = DataSetUtil.GetFldStr(lo_ds, "UPDMS_REF_REPORT", "HomeRun_Yn_Icon");
					lbl_satisfaction.Text = DataSetUtil.GetFldStr(lo_ds, "UPDMS_REF_REPORT", "Satisfaction");
					txt_contents.Text = DataSetUtil.GetFldStr(lo_ds, "UPDMS_REF_REPORT", "Contents");
					lbl_keyword.Text = DataSetUtil.GetFldStr(lo_ds, "UPDMS_REF_REPORT", "Keyword");
					lbl_attach_file_info.Text = DataSetUtil.GetFldStr(lo_ds, "UPDMS_REF_REPORT", "Attach_File_Info");
				}
			}

			SetAdminMode("VIEW");
		}

		#endregion

		#region ClearDetail

		private void ClearDetail()
		{
			lbl_shop_nm.Text = "";
			lbl_nick_nm.Text = "";
			lbl_tour_dt.Text = "";
			lbl_tour_time.Text = "";
			lbl_place.Text = "";
			lbl_location.Text = "";
			lbl_price.Text = "";
			lbl_homerun_yn.Text = ""; 
			lbl_satisfaction.Text = "";
			txt_contents.Text = "";
			lbl_keyword.Text = "";

			SetAdminMode("ADMN");
		}

		#endregion

		#region Page_PreRender

		// Page_PreRender
		private void Page_PreRender(object sender, System.EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				using (AccessCheck access = new AccessCheck())
				{
					//접근권한 체크
					if (!access.CheckPgmAccessGrant(PID, USER_ID, ACCESS_LOG_INFO))
					{
						Response.Redirect(URL_NO_AUTHORITY_REDIRECT_BASE);	//사용자의 권한은 요청하신 페이지를 볼 수 없습니다
					}
					//사용권한별 버튼처리
					PagePreRender.SetVisibleCtrl(this, access.CheckPgmUsingGrant(USER_DIV, PID), access.GetVisibleCtrl(PID));
				}
			}
			SetGridViewHeader(global.GetColumnListByPgmId(PID, LANG_SET));
		}

		#endregion

		#region GridView이벤트

		//RowDataBound
		protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			GridView gv = (GridView)sender;

			if (e.Row.RowType == DataControlRowType.DataRow)
			{
				//연도별로 보는경우 (홀짝수)월별로 셀 색깔을 다르게 함
				for (int i=0; i<gv.Columns.Count; i++)
				{
					//문자열에서 월을 추출한다.
					//<span class="dt color_red">01/06</span><br> => 01/06
					//<span class="dt">02</span><br><div class="hand" onclick="viewDetail('98')">●XXX<span class="sicon_heart_s"></span></div>
					string cell_data = e.Row.Cells[i].Text;
					string tmp = ConversionUtil.ExtractStrBtwnChar(cell_data, "dt\">", "</span><br />");
					string [] arr_month;
					int month = 1;
					
					if (tmp.Contains("/"))
					{
						arr_month = tmp.Split('/');
						if (arr_month.Length > 1)
						{
							month = Convert.ToInt32(arr_month[0].ToString());
						}

						//짝수월이면
						string td_class = ((month % 2) == 0) ? "v_align_t bg_bluesky" : "v_align_t bg_white";
						e.Row.Cells[i].Attributes.Add("class", td_class);
					}
					else
					{
						e.Row.Cells[i].Attributes.Add("class", "v_align_t");
					}
				}
			}
		}

		#endregion

		#region 버튼이벤트

		//검색
		protected void ibtn_search_Click(object sender, EventArgs e)
		{
			SearchProcess();
		}

		//이전달
		protected void ibtn_prev_month_Click(object sender, EventArgs e)
		{
			SetPrevNextMonth(-1);
			SearchProcess();
		}

		//다음달
		protected void ibtn_next_month_Click(object sender, EventArgs e)
		{
			SetPrevNextMonth(1);
			SearchProcess();
		}

		//당월
		protected void ibtn_this_month_Click(object sender, EventArgs e)
		{
			SetThisMonth();
			SearchProcess();
		}

		//이전년
		protected void ibtn_prev_year_Click(object sender, EventArgs e)
		{
			SetPrevNextYear(-1);
			SearchProcess();
		}

		//다음년
		protected void ibtn_next_year_Click(object sender, EventArgs e)
		{
			SetPrevNextYear(1);
			SearchProcess();
		}

		//당년
		protected void ibtn_this_year_Click(object sender, EventArgs e)
		{
			SetThisYear();
			SearchProcess();
		}

		//상세보기
		protected void lbtn_view_detail_Click(object sender, EventArgs e)
		{
			ViewDetail(Convert.ToInt32(h_seq.Value));
		}

		//관리자비번 입력
		protected void lbtn_admin_pw_Click(object sender, EventArgs e)
		{
			//관리자비번이 맞으면 Contents, Keyword를 보여준다.
			using (AccessCheck access = new AccessCheck())
			{
				if (access.IsAdministrator(USER_ID, txt_admin_pw.Text.Trim()))
				{
					SetAdminMode("ADMN");
				}
				else
				{
					CallMsgBoxInAjax("MSG_ERR_129");	//관리자 비밀번호가 아닙니다
				}
			}
		}

		#endregion
	}
}